package com.yisu.auction.api.core.service;


import com.yisu.auction.api.core.mapper.PrizeArchiveMapper;
import com.yisu.auction.api.core.mapper.PrizeMapper;
import com.yisu.auction.api.core.mapper.SmsMapper;
import com.yisu.auction.api.core.mapper.SystemNoticeMapper;
import com.yisu.auction.api.domain.config.SystemConfig;
import com.yisu.auction.ewf.entity.Prize;
import com.yisu.auction.ewf.entity.PrizeArchive;
import com.yisu.auction.ewf.entity.Sms;
import com.yisu.auction.ewf.entity.SystemNotice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by xlt on 2017/4/24.
 */
@Service
public class BaseService {
    @Resource
    public SystemConfig systemConfig;
    @Resource
    private SmsMapper smsMapper;
    @Resource
    private PrizeMapper prizeMapper;
    @Resource
    private PrizeArchiveMapper prizeArchiveMapper;
    @Resource
    private SystemNoticeMapper systemNoticeMapper;

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());


    /**
     * 通过prizeId获取竞品
     *
     * @param prizeId 竞品Id
     * @return
     */
    public Prize getPrizeById(long prizeId) {
        Prize prize = prizeMapper.selectByPrimaryKey(prizeId);
        if (prize == null) {
            PrizeArchive prizeArchive = prizeArchiveMapper.selectByPrimaryKey(prizeId);
            if (prizeArchive != null)
                prize = new Prize(prizeArchive);
        }
        return prize;
    }

    /**
     * 添加消息推送
     *
     * @param content  消息内容
     * @param mobile   手机号
     * @param sendType 发送类型
     * @param memberId 会员ID
     * @param prizeId  竞品ID
     * @param term     期号
     * @param sendTime 发送时间
     * @return
     */
    public boolean addSmsPush(String content, String mobile, byte sendType, long memberId, long prizeId, int term, Date sendTime) {
        Sms sms = new Sms();
        sms.setIsValid(true);
        sms.setContent(content);
        sms.setMobile(mobile);
        sms.setSendType(sendType);
        sms.setMemberId(memberId);
        sms.setPrizeId(prizeId);
        sms.setSendTime(sendTime);
        sms.setStatus((byte) 0);
        sms.setCreateDate(new Date());
        smsMapper.insertSelective(sms);
        return true;
    }

    /**
     * 添加消息推送
     *
     * @param content  消息内容
     * @return
     */
    public boolean addSystemNotice(String content, long toMemberId,String title,String contentAbstract,int msgType) {
        SystemNotice systemNotice= new SystemNotice();
        systemNotice.setContent(content);
        systemNotice.setToMemberId(toMemberId);
        systemNotice.setTitle(title);
        systemNotice.setContentAbstract(contentAbstract);
        systemNotice.setMsgType(msgType);
        systemNotice.setCreateDate(new Date());
        systemNoticeMapper.insertSelective(systemNotice);
        return true;
    }
}
